package com.filetransfer.controller;

import com.filetransfer.model.dto.MessageDTO;
import com.filetransfer.model.vo.ApiResult;
import com.filetransfer.service.MessageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息控制器
 */
@Slf4j
@RestController
@RequestMapping("/message")
@RequiredArgsConstructor
public class MessageController {

    private final MessageService messageService;
    
    /**
     * 发送消息
     */
    @PostMapping("/send")
    public ApiResult<Long> send(@RequestBody MessageDTO messageDTO) {
        Long messageId = messageService.sendMessage(messageDTO);
        return ApiResult.success(messageId);
    }
    
    /**
     * 获取两个设备之间的消息
     */
    @GetMapping("/between")
    public ApiResult<List<MessageDTO>> getMessagesBetween(
            @RequestParam String deviceId1,
            @RequestParam String deviceId2) {
        List<MessageDTO> messages = messageService.getMessagesBetween(deviceId1, deviceId2);
        return ApiResult.success(messages);
    }
    
    /**
     * 获取未读消息数
     */
    @GetMapping("/unread/count")
    public ApiResult<Integer> getUnreadCount(@RequestParam String receiverId) {
        int count = messageService.getUnreadCount(receiverId);
        return ApiResult.success(count);
    }
    
    /**
     * 标记消息为已读
     */
    @PutMapping("/read/{messageId}")
    public ApiResult<Boolean> markAsRead(@PathVariable Long messageId) {
        boolean result = messageService.markAsRead(messageId);
        return ApiResult.success(result);
    }
    
    /**
     * 批量标记消息为已读
     */
    @PutMapping("/read/all")
    public ApiResult<Integer> markAllAsRead(
            @RequestParam String senderId,
            @RequestParam String receiverId) {
        int count = messageService.markAllAsRead(senderId, receiverId);
        return ApiResult.success(count);
    }
    
    /**
     * 更新消息状态
     */
    @PutMapping("/status")
    public ApiResult<Boolean> updateStatus(@RequestParam Long messageId, @RequestParam Integer status) {
        boolean result = messageService.updateStatus(messageId, status);
        return ApiResult.success(result);
    }
} 